#!/bin/bash
# Hive元数据监控系统卸载脚本

set -e

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

echo_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

echo_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查是否为root用户
if [ "$EUID" -ne 0 ]; then 
    echo_error "请使用root用户或sudo执行此脚本"
    exit 1
fi

echo_warn "即将卸载Hive元数据监控系统"
read -p "确认继续？(y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
    echo_info "取消卸载"
    exit 0
fi

# 1. 停止服务
echo_info "停止服务..."
systemctl stop hive-metadata-monitor || true
systemctl stop hive-binlog-monitor || true

# 2. 禁用服务
echo_info "禁用服务..."
systemctl disable hive-metadata-monitor || true
systemctl disable hive-binlog-monitor || true

# 3. 删除systemd服务文件
echo_info "删除systemd服务文件..."
rm -f /etc/systemd/system/hive-metadata-monitor.service
rm -f /etc/systemd/system/hive-binlog-monitor.service

# 4. 重载systemd
systemctl daemon-reload

# 5. 删除日志轮转配置
echo_info "删除日志轮转配置..."
rm -f /etc/logrotate.d/hive-metadata-monitor

# 6. 询问是否删除安装目录
read -p "是否删除安装目录 /opt/hive-metadata-monitor？(y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo_info "删除安装目录..."
    rm -rf /opt/hive-metadata-monitor
fi

# 7. 询问是否删除日志
read -p "是否删除日志文件 /var/log/hive-metadata-monitor？(y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo_info "删除日志文件..."
    rm -rf /var/log/hive-metadata-monitor
fi

# 8. 删除运行时目录
echo_info "删除运行时目录..."
rm -rf /var/run/metadata-monitor

echo_info "卸载完成！"

